<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">

		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<title></title>
		<link rel="stylesheet" href="css/mui.min.css" />
		<link href="css/myapp.css" rel="stylesheet" />
		<style>
			#pullrefresh {
				text-align: center;
				margin-top: -200px;
				width: 100%;
				height: 500px;
				line-height: 50px;
				font-weight: 100;
				color: #000000;
			}
		</style>
	</head>

	<body class="loginbg">
		<div class="mui-content mui-content-logindiv">
			<div class="logo-div">
				<img src="images/tx-login1.jpg" height="100%" style="border-radius: 50px;" />
			</div>
			<form id='login-form' class="mui-input-group login-form" action="" enctype="multipart/form-data" method="post">
				<div class="mui-input-row mui-input-row-login">
					<label><span class="mui-icon mui-icon-person"></span></label>
					<input id='account' name="phonenumber" type="text" class="mui-input-clear mui-input" style="-webkit-text-fill-color: #C5E2FF;" placeholder="请输入账号" onblur="checkAccount()" maxlength="11" >
				</div>
				<div class="mui-input-row mui-input-row-login">
					<label><span class="mui-icon mui-icon-locked"></span></label>
					<input id='password' name="password" type="password" class="mui-input-clear mui-input" style="-webkit-text-fill-color: #C5E2FF;" placeholder="请输入密码">
				</div>

			</form>
			<div class="mui-content-padded mui-content-login">
				<span id="loginStatus"></span>
				<button id='login' class="mui-btn mui-btn-block mui-btn-primary mui-btn-login">登录</button>
				<div class="link-area">
					<a href="#" id='reg'>注册账号</a> <span class="spliter">|</span>
					<a href="#" id='forgetPassword'>忘记密码</a>
				</div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/websqlwrapper.js"></script>
		<script src="js/constant.js"></script>
		<script src="js/websqldb.js"></script>
		<script src="js/jquery-1.9.1.js"></script>
		<script>
			function checkAccount() {
				var phone = document.getElementById('account').value;
				if(phone != '') {
					if(!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) {
						mui.toast("输入的手机号不合法，请重新输入");
						document.getElementById('account').focus();
						return false;
					}
				}

			}
			window.onload = function() {
				var userName = mui("#account")[0];
				var pass = mui("#password")[0];
				if(localStorage.getItem("phoneNumber") && localStorage.getItem("password")) {
					userName.value = localStorage.getItem("phoneNumber");
					pass.value = localStorage.getItem("password");
					if(localStorage.getItem("status") == 0) {
						mui.ajax(Url+"/user/login", {
							data: {
								account: userName.value,
								password: pass.value
							},
							dataType: 'json', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							success: function(data) {
								if(!data.status) {
									//mui.toast("账号或者密码错误");
								}
								localStorage.setItem("phoneNumber", data.data[0].account);
								localStorage.setItem("password", data.data[0].password);
								localStorage.setItem("nickName", data.data[0].user_name);
								localStorage.setItem("userImg", data.data[0].img);
								mui.openWindow({
									url: "/examples/tab-webview-main.html",
									id: "tab-webview-main.html",
									createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
								})
							},
							error: function(xhr, type, errorThrown) {
								w.close();
								mui.toast("网络异常");
							}
						});
					}
				}
				//点击登录
				document.getElementById("login").addEventListener('tap', function() {
					if(plus.networkinfo.getCurrentType() == 0 || plus.networkinfo.getCurrentType() == 1) {
					//遮罩
					var w = plus.nativeUI.showWaiting("加载中...");
						setTimeout(function() {
							w.close();
						}, 2000)
						mui.toast("网络异常");
					}
					checkAccount();
					if(mui("#account")[0].value == "" || mui("#password")[0].value == "") {
						mui.toast("用户名或者密码不能为空");
						return false;
					}else{
					//遮罩
					var w = plus.nativeUI.showWaiting("加载中...");
						mui.ajax(Url+"/user/login", {
							data: {
								account: mui("#account")[0].value,
								password: mui("#password")[0].value
							},
							dataType: 'json', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							timeout: 2000, //超时时间设置为10秒；
							success: function(data) {
								if(!data.status) {
									mui.toast("账号或者密码错误");
									setTimeout(function() {
										w.close();
									}, 2000);
								} else if(data.status) {
									localStorage.setItem("phoneNumber", data.data[0].account);
									localStorage.setItem("password", data.data[0].password);
									localStorage.setItem("nickName", data.data[0].user_name);
									localStorage.setItem("userImg", data.data[0].img);
									localStorage.setItem("status", 0);
									mui.openWindow({
										url: "/examples/tab-webview-main.html",
										id: "tab-webview-main.html",
										createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
									})
									w.close();
								}
							},
							error: function(xhr, type, errorThrown) {
								setTimeout(function() {
									w.close();
								}, 2000)
								mui.toast("网络异常");
							}
						});
					}
				});
			}
				/*
				 * author lsj
				 * login 页面进注册页面入口
				 */
			document.getElementById("reg").addEventListener('tap', function() {
				mui.openWindow({
					url: "reg.html",
					id: "reg.html",
					createNew: false //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
				})
			})
		</script>
	</body>

</html>